<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 订单
 */

namespace app\api\controller\user\v1_0\bus;


use think\Db;
class Alipay extends \app\api\controller\user\v1_0\Common
{
    protected $noNeedRight = ['test'];
     /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 支付宝默认信息
     */
    public function index(){
        $user = (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $data = (new \app\common\model\BusAlipay())->where(['bus_id' => $user['bus_id']])->find();
        return self::successResult("成功",$data);
    }
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 绑定支付宝
     */
    public function alipay(){
        $param = $this->request->param();
        $validate = new \app\common\validate\BusAlipay();
        $result = $validate->scene("add")->check($param);
        if ($result === false) {
            return self::errorResult($validate->getError());
        }
        $user = (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $data = (new \app\common\model\BusAlipay())->where(['bus_id' => $user['bus_id']])->find();
        if($data){
            $data = (new \app\common\model\BusAlipay())->where(['bus_id' => $user['bus_id']])->update([
                'name' => $param['name'],
                'phone' => $param['phone'],
                'notes' => $param['notes'],
            ]);
        }else{
            $data = (new \app\common\model\BusAlipay())->save([
                'name' => $param['name'],
                'phone' => $param['phone'],
                'bus_id' => $user['bus_id'],
                'notes' => $param['notes'],
            ]);
        }
          return self::successResult("成功");
    }
    

}
